package bussiness.core;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Game;
import model.Post;

import database.dao.GameDao;
import database.dao.PostDao;

/**
 * Servlet implementation class PostList
 */
public class PostList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostList() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = 0;
		String s_pid = request.getParameter("pid");
		int pid = 1;
		try {
			id = Integer.parseInt(request.getParameter("id"));
		} catch(Exception e ){
			id = 1;
		}
		if(s_pid != null) {
			pid = Integer.parseInt(s_pid);
		}
		Post[] posts = PostDao.getPosts(id, pid);
		request.setAttribute("posts", posts);
		Game g = GameDao.getGameById(id);
		request.setAttribute("game", g);
		RequestDispatcher view = request.getRequestDispatcher("WEB-INF/jsp/postlist.jsp");
		view.forward(request, response);
	}
}
